/*
 * @(#)LoginFilter.java 2008-3-5
 * 
 * Copyright 2007 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */
package com.cnc.beanutils;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cnc.bas.util.Constants;

/**
 * @author LW
 * @date   2009-6-16
 * @version 1.0
 */
public class LoginFilter implements Filter {
	private FilterConfig config;

	public FilterConfig getConfig() {
		return config;
	}

	public void setConfig(FilterConfig config) {
		this.config = config;
	}

	public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException{
		if (this.config == null|| !(request instanceof HttpServletRequest)) return;		
		HttpServletRequest  req = (HttpServletRequest)request;	
		String uri = req.getRequestURI();		
		boolean isLogon = uri.endsWith("logon.do")||uri.endsWith("index.do")||uri.endsWith("logoff.do");			
		if(!isLogon){
		    HttpSession session = req.getSession(false);		  
		    if(session == null||session.getAttribute(Constants.USER_KEY)==null){
			HttpServletResponse  ros = (HttpServletResponse)response;
			String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+"/timeout.jsp";
			ros.sendRedirect(basePath);
			return;
		    }			
		}
		chain.doFilter(request, response);
	}
	
	public void destroy(){
		this.config = null;
	}
	public void init(FilterConfig config) throws ServletException {		
		this.config = config;		
		
	}

}


